﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using Rsa.moodleWebService;
using System.Diagnostics;


namespace Rsa
{
    class GestorMoodle
    {
        private GestorConfigXML Gxml = null;
        private List<Lista> _listaMoodle = null;
        private string _username=null;
        private string _password=null;
        private string _key=null;
        public string Resultado=null;



        public GestorMoodle(GestorConfigXML gx)
        {
            Gxml = gx;
            this.loadConfigMoodle();
        }


        public void start(int opcao)
        {
            if (opcao == 0)
            {
                //opcao 1

            }
            else if (opcao == 1)
            {
                //opcao 2

            }
            else if (opcao == 2)
            {
                //opcao 3
            }

            this.loadConfigMoodle();
            if (string.IsNullOrWhiteSpace(Password) || string.IsNullOrWhiteSpace(Username))
                return;

            try
            {

                UEDWsServiceService srv = new UEDWsServiceService();
                Key = srv.getAuthorizationKey(Username, Password, "2");


                UedCredentials cred = new UedCredentials();
                cred.autorizationKey = Key;
                cred.username = Username;

                UedCourseFull[] res = srv.getMyCourses(cred);


                UedDate uedData = new UedDate();
                DateTime dataStart = DateTime.Now;
                uedData.day = dataStart.Day.ToString();
                uedData.month = dataStart.Month.ToString();
                uedData.year = dataStart.Year.ToString();

                UedDate tst = srv.unixTimestampToUedDate(cred, "2012-01-01");             

                ListaMoodle = new List<Lista>();

                if (opcao == 0)
                {
                    foreach (UedCourseFull i in res)
                    {
                        Lista l = new Lista();
                        l.Titulo = i.fullName;
                        l.Data = new DateTime(int.Parse(i.startDate.year), int.Parse(i.startDate.month), int.Parse(i.startDate.day));
                        l.Imagem = "../../imagens/moodle.png";
                        l.Corpo = "";

                        ListaMoodle.Add(l);
                    }

                }
                else if (opcao == 1)
                {
                    foreach (UedCourseFull i in res)
                    {
                        Console.WriteLine(i.fullName);
                        Lista l = new Lista();
                        l.Titulo = i.fullName;
                        l.Imagem = "../../imagens/moodle.png";
                        l.Corpo = "<meta http-equiv='Content-Type' content='text/html;charset=UTF-8'>";
                        l.Data = new DateTime(int.Parse(i.startDate.year), int.Parse(i.startDate.month), int.Parse(i.startDate.day));


                        UedRecentForumActivity[] ls = srv.getCourseRecentForumActivity(cred, i.id, tst);

                        foreach (UedRecentForumActivity j in ls)
                        {
                            UedForumActivity[] qw = j.activity;
                            l.Corpo += "<strong>" + j.forumName + "</strong><br/>";

                            foreach (UedForumActivity m in qw)
                            {
                                l.Corpo += m.postSubject.ToString() + "<br/>";
                                Debug.WriteLine(m.postSubject.ToString());
                            }
                        }
                        ListaMoodle.Add(l);
                    }
               }
            
            }
            catch (Exception e)
            {
                Console.WriteLine("Start Moodle exception:   " + e);
            }
             

        }


        //carrega a configuração
        public void loadConfigMoodle()
        {
            Hashtable hash = Gxml.loadMoodle();

            Username = (string)hash["username"];
            if (string.IsNullOrEmpty(Username.Trim()))
                Username = null;

            Password = (string)hash["password"];
            if (string.IsNullOrEmpty(Password.Trim()))
                Password = null;
        }



        #region gets e sets

        public string Key
        {
            get { return _key; }
            set { _key = value; }
        }

        public string Password
        {
            get { return _password; }
            set { _password = value; }
        }

        public string Username
        {
            get { return _username; }
            set { _username = value; }
        }

       

        public List<Lista> ListaMoodle
        {
            get { return _listaMoodle; }
            set { _listaMoodle = value; }
        }

        #endregion



        public UedRecentForumActivity[] ls { get; set; }

        public UedForumActivity[] qw { get; set; }
    }
}
